import * as BABYLON from "babylonjs";
import * as GUI from 'babylonjs-gui'

import type { Mesh } from "babylonjs";

// import Base from "../Base";


export default class Agv {
    // private base: Base;
    private ip: string
    public agv: Mesh
    public labelObj: TTextBlockObj
    private panelPos: TPanelPos

    constructor(ip: string, panelPos: TPanelPos) {
        // this.base = Base.getInstance()
        this.ip = ip
        this.panelPos = panelPos
        this.agv = this.createAgv()
        this.labelObj = this.createLabel(this.agv)
    }

    private createAgv() {
        const faceUV = [];
        faceUV[0] = new BABYLON.Vector4(0, 0, 0, 0); //rear face
        faceUV[1] = new BABYLON.Vector4(0, 0, 0, 0); //front face
        faceUV[2] = new BABYLON.Vector4(0, 0, 0, 0); //right side
        faceUV[3] = new BABYLON.Vector4(0, 0, 0, 0); //left side
        faceUV[4] = new BABYLON.Vector4(0, 0, 1.0, 1.0); //top side
        faceUV[5] = new BABYLON.Vector4(0, 0, 0, 0); //down side

        const boxMat = new BABYLON.StandardMaterial("boxMat");
        boxMat.diffuseTexture = new BABYLON.Texture("/arrow.svg");
        boxMat.emissiveTexture = new BABYLON.Texture("/white.svg")
        boxMat.disableLighting = true

        const box = BABYLON.MeshBuilder.CreateBox("agv", {
            faceUV: faceUV,
            width: 1,
            height: 0.3
        })
        box.material = boxMat;
        box.position.y = 0.5

        return box
    }

    private createLabel(agv: Mesh) {

        const advancedTexture = GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");

        // 创建一个容器
        const panel = new GUI.Rectangle();
        panel.width = "90px";
        panel.height = "90px";
        panel.cornerRadius = 3;
        panel.color = "Green";
        panel.thickness = 1;
        panel.background = "rgba(255,255,255,0.8)";
        panel.linkOffsetY = this.panelPos.linkOffsetY;
        panel.linkOffsetX = this.panelPos.linkOffsetX;
        advancedTexture.addControl(panel);
        panel.linkWithMesh(this.agv);

        // 绿色圆点   🟢 

        // header label
        const ip = new GUI.TextBlock();
        // position
        ip.textHorizontalAlignment = GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
        ip.textVerticalAlignment = GUI.Control.VERTICAL_ALIGNMENT_TOP;
        ip.top = 10
        ip.left = 5
        // content
        ip.text = this.ip
        ip.fontSize = "9px";
        ip.fontWeight = "bold"
        ip.color = '#058732'
        panel.addControl(ip);



        // status label --------------------------------------------------------------
        const status = new GUI.TextBlock();
        // position
        status.textHorizontalAlignment = GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
        status.textVerticalAlignment = GUI.Control.VERTICAL_ALIGNMENT_TOP;
        status.top = 30
        status.left = 12
        // content
        status.text = "🔴: 离线"
        status.fontSize = "11px";
        status.color = '#058732'
        status.fontWeight = "bold"
        panel.addControl(status);

        // battery label--------------------------------------------------------------
        const battery = new GUI.TextBlock();
        // position
        battery.textHorizontalAlignment = GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
        battery.textVerticalAlignment = GUI.Control.VERTICAL_ALIGNMENT_TOP;
        battery.top = 50
        battery.left = 14
        // content
        battery.text = "🔋: 99.99% "
        battery.fontSize = "12px";
        battery.color = '#058732'
        battery.fontWeight = "bold"
        panel.addControl(battery);

        // speed label 
        const speed = new GUI.TextBlock();
        // position
        speed.textHorizontalAlignment = GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
        speed.textVerticalAlignment = GUI.Control.VERTICAL_ALIGNMENT_TOP;
        speed.top = 70
        speed.left = 11
        // content
        speed.fontSize = "12px";
        speed.fontWeight = "bold"
        speed.text = "️🚗: 0.00m/s"
        speed.color = '#058732'
        panel.addControl(speed);


        const line = new GUI.Line();
        line.lineWidth = 2;
        line.color = "White";
        line.connectedControl = panel;
        // 设置终点的位置
        line.y2 = 40
        line.x2 = -40
        advancedTexture.addControl(line);
        line.linkWithMesh(agv);

        return {
            ip,
            status,
            battery,
            speed
        }

    }

    public moveAgv(x: number, y: number, angle: number) {
        this.agv.position = new BABYLON.Vector3(x, 0.5, y)
        this.agv.rotation.y = -angle
    }


    public changeLabel(labelObj: TLabel) {

        this.labelObj.battery.text = `🔋:${(labelObj.battery * 100).toFixed(2)}%`
        this.labelObj.speed.text = `️🚗:${labelObj.speed.toFixed(2)}m/s`

        // 🟢:  在线     🔴: 离线
        if (labelObj.status) {
            this.labelObj.status.text = "🔵: 在线"
        } else {
            this.labelObj.status.text = "🔴: 离线"
        }

    }


}


